﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using XML_Programming_Assignment.Class;
using System.Xml;
using System.Xml.Schema;

namespace XML_Programming_Assignment
{
    public partial class FrmCheckStocksLevel : Form
    {
        public FrmCheckStocksLevel()
        {
            InitializeComponent();
        }   

        private void FrmCheckStocksLevel_Activated(object sender, EventArgs e)
        {
            bool chkUnitsToOrder = false;
            bool chkCurrentPrice = false;

            dgvCurrentStockLevel.Rows.Clear();

            foreach (DataGridViewColumn c in dgvCurrentStockLevel.Columns)
            {
                if (c.HeaderText == "Units To Order")
                {
                    chkUnitsToOrder = true;
                }
                if (c.HeaderText == "Current Price")
                {
                    chkCurrentPrice = true;
                }                
            }

            if (chkUnitsToOrder == false)
            {
                DataGridViewTextBoxColumn ColUnitsToOrder = new DataGridViewTextBoxColumn();
                ColUnitsToOrder.Name = "ColUnitsToOrder";
                ColUnitsToOrder.HeaderText = "Units To Order";
                ColUnitsToOrder.ReadOnly = true;
                ColUnitsToOrder.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                dgvCurrentStockLevel.Columns.Add(ColUnitsToOrder);
                dgvCurrentStockLevel.Columns["ColUnitsToOrder"].DisplayIndex = 4;
            }

            if (chkCurrentPrice == false)
            {
                DataGridViewTextBoxColumn ColCurrentPrice = new DataGridViewTextBoxColumn();
                ColCurrentPrice.Name = "ColCurrentPrice";
                ColCurrentPrice.HeaderText = "Current Price";
                ColCurrentPrice.ReadOnly = true;
                ColCurrentPrice.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                dgvCurrentStockLevel.Columns.Add(ColCurrentPrice);
                dgvCurrentStockLevel.Columns["ColCurrentPrice"].DisplayIndex = 5;
            }

            foreach (Product product in AllFormClass.ProductList)
            {
                dgvCurrentStockLevel.Rows.Add(null, product.ProductID1, product.ProductName1, product.UnitsInStock1, product.UnitsToOrder1, product.CurrentPrice1);
            }            
        }

        private void chkShowAllStocks_CheckStateChanged(object sender, EventArgs e)
        {
            ArrayList result = null;           

            dgvCurrentStockLevel.Rows.Clear();

            if (chkShowAllStocks.CheckState.ToString() == "Checked")
            {
                result = DBManager.AllProductRetrieve();
            }
            else
            {                
                result = DBManager.LowProductStockRetrieve();
            }
            foreach (Product product in result)
            {
                //dgvCurrentStockLevel.Rows.Add(null, product.ProductID1, product.ProductName1, product.UnitsInStock1, "Add Notes");
                dgvCurrentStockLevel.Rows.Add(null, product.ProductID1, product.ProductName1, product.UnitsInStock1, product.UnitsToOrder1, product.CurrentPrice1);
            }             
        }

        private void btnSendRFQ_Click(object sender, EventArgs e)
        {
            //confirmation to send the RFQ
            if (MessageBox.Show("Do you want to send this RFQ?", "Confirm File Sending", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                ArrayList rfqList = new ArrayList();
                ArrayList rfqItemList = new ArrayList();

                bool checkProduct = false;

                if (tbxRFQNumber.Text == "")
                {
                    MessageBox.Show("Please enter RFQ Number.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    int RFQNumber = Convert.ToInt32(tbxRFQNumber.Text);

                    string date = Convert.ToString(DateTime.Now.ToString("dd/MM/yyyy"));
                    RFQ rfq = null;
                    foreach (DataGridViewRow r in dgvCurrentStockLevel.Rows)
                    {
                        DataGridViewCell selectCell = r.Cells["ColSelect"];
                        if (selectCell.Value != null)
                        {
                            if (((bool)selectCell.Value) == true)
                            {
                                //Product product= new Product(r.Cells["ProductID"], r.Cells["ProductName"], r.Cells[
                                string unitstoorder = r.Cells["ColUnitsToOrder"].ToString();
                                RFQItem rfqItem = new RFQItem(RFQNumber, r.Cells["ColItemID"].Value.ToString(), Convert.ToInt32(r.Cells["ColUnitsToOrder"].Value), Convert.ToDouble(r.Cells["ColCurrentPrice"].Value));
                                rfqItemList.Add(rfqItem);

                                checkProduct = true;
                            }

                        }
                    }

                    if (checkProduct != false)
                    {

                        string note = "";
                        if (chkAddNote.CheckState.ToString() == "Checked")
                        {
                            note = tbxNote.Text;
                        }
                        else
                        {
                            note = " ";
                        }
                        rfq = new RFQ(RFQNumber, date, 9888, note);
                        rfqList.Add(rfq);

                        //create the xml file for storing the rfq
                        XmlWriterSettings settings = new XmlWriterSettings();
                        settings.Indent = true;

                        XmlWriter wrt = XmlWriter.Create("C:\\SharedDatabase\\RFQ_G1.xml", settings);
                        settings.Indent = false;

                        wrt.WriteStartDocument();
                        wrt.WriteStartElement("RFQs");
                        string xs = "http://www.w3.org/2001/XMLSchema-instance";
                        wrt.WriteAttributeString("xsi", "noNamespaceSchemaLocation", xs, "RFQSchema.xsd");


                        //iterate through the list of products
                        foreach (RFQ rfq1 in rfqList)
                        {
                            wrt.WriteStartElement("RFQ");
                            wrt.WriteAttributeString("RFQNumber", Convert.ToString(rfq1.RFQNumber));
                            wrt.WriteElementString("Date", rfq1.RFQDate);
                            wrt.WriteElementString("BuyerID", Convert.ToString(rfq1.BuyerID));
                            wrt.WriteElementString("Notes", rfq1.Notes);

                            wrt.WriteStartElement("Items");
                            foreach (RFQItem rfqitem in rfqItemList)
                            {
                                if (rfqitem.RfqNumber == rfq1.RFQNumber)
                                {
                                    wrt.WriteStartElement("Item");
                                    wrt.WriteAttributeString("Id", Convert.ToString(rfqitem.RfqItemID));
                                    wrt.WriteElementString("Quantities", Convert.ToString(rfqitem.Quantities));
                                    wrt.WriteElementString("UnitPrice", Convert.ToString(rfqitem.UnitPrice));
                                    wrt.WriteEndElement();
                                }
                            }
                            wrt.WriteEndElement();
                            wrt.WriteEndElement();
                        }

                        //close the file
                        wrt.WriteEndElement();
                        wrt.Flush();
                        wrt.Close();

                        //inform user the saving is completed
                        MessageBox.Show("RFQ is sent successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);


                    }
                    else
                    {
                        MessageBox.Show("Please choose at least one product to generate RFQ.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                //Reset display
                tbxRFQNumber.Clear();
                chkAddNote.Checked = false;
                chkShowAllStocks.Checked = false;
                tbxNote.Clear();
            }
            else
            {

            }
        }

        private void chkAddNote_CheckStateChanged(object sender, EventArgs e)
        {
            if (chkAddNote.CheckState.ToString() == "Checked")
            {
                tbxNote.ReadOnly = false;
            }
            else
            {
                tbxNote.ReadOnly = true;
            }
        }

        private void btnGenerateRFQ_Click(object sender, EventArgs e)
        {
            lblDate.Text = Convert.ToString(DateTime.Now.ToString("dd-MM-yyyy"));
            gbQuotation.Visible = true;  
        }

        private void MainMenu_toolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            AllFormClass.formDefault.Show();
        }

        private void ReadQuotation_toolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            AllFormClass.formReadQuotation.Show();
        }

        private void Validate_toolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
            AllFormClass.formRFQValidation.Show();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("David Computer Service Pte Ltd" + "\r\n" + "Supply Chain Management System" + "\r\n" + "Developed By" + "\r\n" + "Sithu Kyaw", "Version", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void notepadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("Notepad.exe");
        }

        private void calculatorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("calc");
        }
    }
}
